/*
 * OneHundredBottles.java
 * 
 * Created on Jun 19, 2007, 7:50:34 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flyingsaucerpdf;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

/**
 *
 * @author joshy
 */
public class OneHundredBottles {

public static void main(String[] args) throws Exception {
    
    StringBuffer buf = new StringBuffer();
    buf.append("<html>");
    
    // put in some style
    buf.append("<head><style language='text/css'>");
    buf.append("h3 { border: 1px solid #aaaaff; background: #ccccff; ");
    buf.append("padding: 1em; text-transform: capitalize; font-family: SimSun; font-weight: normal;}");
    buf.append("p { margin: 1em 1em 4em 3em; } p:first-letter { color: red; font-size: 150%; }");
    buf.append("h2 { background: #5555ff; color: white; border: 10px solid black; padding: 3em; font-size: 200%; }");
    buf.append("</style></head>");
    
    // generate the body
    buf.append("<body>");
    buf.append("<p><img src='samples/100bottles.jpg'/></p>");
    for(int i=99; i>0; i--) {
        buf.append("<h3>你好"+i+" bottles of beer on the wall, "
                + i + " bottles of beer!</h3>");
        buf.append("<p>Take one down and pass it around, "
                + (i-1) + " bottles of beer on the wall</p>\n");
    }
    buf.append("<h2>No more bottles of beer on the wall, no more bottles of beer. ");
    buf.append("Go to the store and buy some more, 99 bottles of beer on the wall.</h2>");
    buf.append("</body>");
    buf.append("</html>");
    // parse the markup into an xml Document
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new StringBufferInputStream(buf.toString()));

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(doc, null);
    // 解决中文支持问题  
    ITextFontResolver fontResolver = renderer.getFontResolver();  
    fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  
    String outputFile = "100bottles.pdf";
    OutputStream os = new FileOutputStream(outputFile);
    renderer.layout();
    renderer.createPDF(os);
    os.close();
}
}
